<?php

namespace app\common\service;

use app\common\service\Base;
use think\Db;

class Purchasing extends Base
{
    /**
     * 统计数据
     *2020/10/31
     * @return array
     */
    public function getPurchasingStat($factoryId = 0, $where = [], $detailWhere = [])
    {
        $cacheSuffix = ADMIN_ID;
        $allCount = $fabricCgOverCount = $fabricCgNotCount = $approvalColorOverCount = $approvalColorNotCount = $approvalColorNoCount = $checkcloShrinkOverCount = $caichuangOverCount = $caichuangNotCount = $AcOverCount = $AcNotCount = $AcNoCount = 0;
        $where[] = ['stock_type', 'not in', '10,11'];
        //总下单数
        $allCount = db('order')->where($where)->count('id');
        $allOrder = db('order')->where($where)->column('id');
        $allOrders = implode(",", $allOrder) ? implode(",", $allOrder) : 0;

        $whereAll = $whereFabricOver = $whereFabricNot = $whereApOverColor = $whereApNotColor = $whereApNoColor = $whereCheckcloShrinkOver = $whereCheckcloShrinkNot = $whereNeedCheckcloShrink = $whereCaichuangOver = $whereCaichuangNot = $whereAcOver = $whereAcNot = $whereAcNo = $returnWhere = $where;
        //退料中单数
        $subQuery2 = db('material_return')->where("status=1 and order_id in ({$allOrders})")->distinct(true)->field('order_id')->buildSql();
        $returnWhere[] = ['id', 'exp', Db::raw("in({$subQuery2})")];
        $returnMaterialCount = db('order')->where($returnWhere)->count('id');
        //面料采购未完成单数
        $subQuery1 = db('order_material om')->join('material m', "om.material_id=m.id", 'left')->where("m.type in ('fabric', 'lining') and om.caigou_show=1 and om.is_make_tag=0 and order_id in ({$allOrders})")->distinct(true)->field('om.order_id')->buildSql();        //dump($subQuery1);
        $whereFabricNot[] = ['id', 'exp', Db::raw("in({$subQuery1})")];
        $whereFabricNot[] = ['id', 'exp', Db::raw("not in({$subQuery2})")];
        $fabricCgNotCount = db('order')->where($whereFabricNot)->count('id');        //dump($fabricCgNotCount);

        //面料采购已完成单数
        $whereFabricOver[] = ['id', 'exp', Db::raw("not in({$subQuery1})")];
        $whereFabricOver[] = ['id', 'exp', Db::raw("not in({$subQuery2})")];
        $fabricCgOverCount = db('order')->where($whereFabricOver)->count('id');//dump(array_intersect($fabricCgNotCount,$returnMaterialCount));
        //未批色通过单数
        $subQuery4 = db('order_material om')->join('material m', "om.material_id=m.id", 'left')->where("m.type in ('fabric', 'lining') and om.caigou_show=1 and om.caigou_status=0")->distinct(true)->field('om.order_id')->buildSql();
        $whereApNotColor[] = ['id', 'exp', Db::raw("not in({$subQuery1})")];
        $whereApNotColor[] = ['id', 'exp', Db::raw("in({$subQuery4})")];
        $approvalColorNotCount = db('order')->where($whereApNotColor)->count('id');

        //批色不通过单数
        $subQuery5 = db('order_material om')->join('material m', "om.material_id=m.id", 'left')->where("m.type in ('fabric', 'lining') and om.caigou_show=1 and om.caigou_status=2")->distinct(true)->field('om.order_id')->buildSql();
        $whereApNoColor[] = ['id', 'exp', Db::raw("not in({$subQuery1})")];
        $whereApNoColor[] = ['id', 'exp', Db::raw("not in({$subQuery4})")];
        $whereApNoColor[] = ['id', 'exp', Db::raw("in({$subQuery5})")];
        $approvalColorNoCount = db('order')->where($whereApNoColor)->count('id');
        //已批色通过单数
        $approvalColorOverCount = $fabricCgOverCount - $approvalColorNotCount;
        //未验布缩水的单数
        $subQuery6 = db('order_material_item omi')->join('order_material om', "omi.order_material_id=om.mid", 'left')->join('material m', "om.material_id=m.id", 'left')->where("m.type in ('fabric', 'lining') and omi.is_return=0 and om.caigou_show=1 and ((om.need_checkcloth=1 and omi.is_checkcloth=0) or (om.need_shrink=1 and omi.is_shrink=0))")->distinct(true)->field('om.order_id')->buildSql();
        $whereCheckcloShrinkNot[] = ['id', 'exp', Db::raw("not in({$subQuery1})")];
        $whereCheckcloShrinkNot[] = ['id', 'exp', Db::raw("in({$subQuery6})")];
        $checkcloShrinkNotCount = db('order')->where($whereCheckcloShrinkNot)->count('id');
        //已验布缩水的单数
        $checkcloShrinkOverCount = $fabricCgOverCount - $checkcloShrinkNotCount;
        //已裁床单数
        $whereCaichuangOver[] = ['id', 'exp', Db::raw("not in({$subQuery1})")];
        $whereCaichuangOver[] = ['id', 'exp', Db::raw("not in({$subQuery2})")];
        $whereCaichuangOver[] = ['id', 'exp', Db::raw("not in({$subQuery4})")];
        $whereCaichuangOver[] = ['id', 'exp', Db::raw("not in({$subQuery5})")];
        $whereCaichuangOver[] = ['caichuang_over_time', '<>', 0];
        $caichuangOverCount = db('order')->where($whereCaichuangOver)->count('id');

        //未裁床单数
        $caichuangNotCount = $approvalColorOverCount - $caichuangOverCount - $approvalColorNoCount;
        //辅料发放单数
        $subQuery9 = db('distributing')->where("status in (1,2,3)")->distinct(true)->field('order_id')->buildSql();
        $whereAcOver[] = ['id', 'exp', Db::raw("not in({$subQuery1})")];
        $whereAcOver[] = ['id', 'exp', Db::raw("not in({$subQuery2})")];
        $whereAcOver[] = ['id', 'exp', Db::raw("not in({$subQuery4})")];
        $whereAcOver[] = ['id', 'exp', Db::raw("not in({$subQuery5})")];
        $whereAcOver[] = ['caichuang_over_time', '<>', 0];
        $whereAcOver[] = ['id', 'exp', Db::raw(" not in({$subQuery9})")];
        $acOverCount = db('order')->where($whereAcOver)->count('id');
        //辅料未发放单数
        $subQuery10 = db('distributing')->where("status in (1,2,3)")->distinct(true)->field('order_id')->buildSql();
        $whereAcNot[] = ['id', 'exp', Db::raw("not in({$subQuery1})")];
        $whereAcNot[] = ['id', 'exp', Db::raw("not in({$subQuery2})")];
        $whereAcNot[] = ['id', 'exp', Db::raw("not in({$subQuery4})")];
        $whereAcNot[] = ['id', 'exp', Db::raw("not in({$subQuery5})")];
        $whereAcNot[] = ['caichuang_over_time', '<>', 0];
        $whereAcNot[] = ['id', 'exp', Db::raw("in({$subQuery10})")];
        $acNotCount = db('order')->where($whereAcNot)->count('id');
        //辅料采购未完成单数
        $subQuery11 = db('order_material om')->join('material m', "om.material_id=m.id", 'left')->where("m.type = 'accessories' and om.caigou_show=1 and om.is_caigou_start!=2")->distinct(true)->field('om.order_id')->buildSql();
        $whereAcNo[] = ['id', 'exp', Db::raw("in({$subQuery11})")];
        $whereAcNo[] = ['status', 'in', [0, 1]];
        $acNoCount = db('order')->where($whereAcNo)->count('id');
        // 整理结果
        $result = array();
        $result['allCount'] = $allCount;
        $result['fabricCgOverCount'] = $fabricCgOverCount;
        $result['returnMaterialCount'] = $returnMaterialCount;
        $result['fabricCgNotCount'] = $fabricCgNotCount;
        $result['apColorOverCount'] = $approvalColorOverCount;
        $result['apColorNotCount'] = $approvalColorNotCount;
        $result['apColorNoCount'] = $approvalColorNoCount;
        $result['ckClothSkOverCount'] = $checkcloShrinkOverCount;
        $result['ckClothSkNotCount'] = $checkcloShrinkNotCount;
        $result['caichuangOverCount'] = $caichuangOverCount;
        $result['caichuangNotCount'] = $caichuangNotCount;
        $result['acOverCount'] = $acOverCount;
        $result['acNotCount'] = $acNotCount;
        $result['acNoCount'] = $acNoCount;
        if ($allCount == 0) {
            $result['fabricCgOverCountPer'] = "0%";
            $result['returnMaterialCountPer'] = "0%";
            $result['fabricCgNotCountPer'] = "0%";
            $result['apColorOverCountPer'] = "0%";
            $result['apColorNotCountPer'] = "0%";
            $result['apColorNoCountPer'] = "0%";
            $result['ckClothSkOverCountPer'] = "0%";
            $result['ckClothSkNotCountPer'] = "0%";
            $result['caichuangOverCountPer'] = "0%";
            $result['caichuangNotCountPer'] = "0%";
            $result['acOverCountPer'] = "0%";
            $result['acNotCountPer'] = "0%";
            $result['acNoCountPer'] = "0%";
        } else {
            $result['fabricCgOverCountPer'] = round($fabricCgOverCount / $allCount * 100, 2) . "%";
            $result['returnMaterialCountPer'] = round($returnMaterialCount / $allCount * 100, 2) . "%";
            $result['fabricCgNotCountPer'] = round($fabricCgNotCount / $allCount * 100, 2) . "%";
            $result['returnMaterialCountPer'] = round($returnMaterialCount / $allCount * 100, 2) . "%";
            if ($fabricCgOverCount == 0) {
                $result['apColorOverCountPer'] = "0%";
                $result['apColorNotCountPer'] = "0%";
                $result['apColorNoCountPer'] = "0%";
                $result['ckClothSkOverCountPer'] = "0%";
                $result['ckClothSkNotCountPer'] = "0%";
            } else {
                $result['apColorOverCountPer'] = round($approvalColorOverCount / $fabricCgOverCount * 100, 2) . "%";
                $result['apColorNotCountPer'] = round($approvalColorNotCount / $fabricCgOverCount * 100, 2) . "%";
                $result['apColorNoCountPer'] = round($approvalColorNoCount / $fabricCgOverCount * 100, 2) . "%";
                $result['ckClothSkOverCountPer'] = round($checkcloShrinkOverCount / $fabricCgOverCount * 100, 2) . "%";
                $result['ckClothSkNotCountPer'] = round($checkcloShrinkNotCount / $fabricCgOverCount * 100, 2) . "%";
            }
            if ($approvalColorOverCount == 0) {
                $result['caichuangOverCountPer'] = "0%";
                $result['caichuangNotCountPer'] = "0%";
            } else {
                $result['caichuangOverCountPer'] = round($caichuangOverCount / $approvalColorOverCount * 100, 2) . "%";
                $result['caichuangNotCountPer'] = round($caichuangNotCount / $approvalColorOverCount * 100, 2) . "%";
            }
            if ($caichuangOverCount == 0) {
                $result['acOverCountPer'] = "0%";
                $result['acNotCountPer'] = "0%";
            } else {
                $result['acOverCountPer'] = round($acOverCount / $caichuangOverCount * 100, 2) . "%";
                $result['acNotCountPer'] = round($acNotCount / $caichuangOverCount * 100, 2) . "%";
            }
            $result['acNoCountPer'] = round($acNoCount / $allCount * 100, 2) . "%";
        }
        $result['detailWhere'] = $detailWhere;
        return $result;
    }
    /**
     * 统计数据
     *2020/10/31
     * @return array
     */
    public function getPurchasingStat_old_v0($factoryId = 0, $where = [], $detailWhere = [])
    {
        $cacheSuffix = ADMIN_ID;
        $allCount = $fabricCgOverCount = $fabricCgNotCount = $approvalColorOverCount = $approvalColorNotCount = $approvalColorNoCount = $checkcloShrinkOverCount = $caichuangOverCount = $caichuangNotCount = $AcOverCount = $AcNotCount = $AcNoCount = 0;
        $where[] = ['stock_type', 'not in', '10,11'];
        //总下单数
        $allCount = db('order')->where($where)->count('id');
        $allOrder = db('order')->where($where)->column('id');
        $allOrders = implode(",", $allOrder) ? implode(",", $allOrder) : 0;

        $whereAll = $whereFabricOver = $whereFabricNot = $whereApOverColor = $whereApNotColor = $whereApNoColor = $whereCheckcloShrinkOver = $whereCheckcloShrinkNot = $whereNeedCheckcloShrink = $whereCaichuangOver = $whereCaichuangNot = $whereAcOver = $whereAcNot = $whereAcNo = $returnWhere = $where;
        //退料中单数
        $subQuery2 = db('material_return')->where("status=1 and order_id in ({$allOrders})")->distinct(true)->field('order_id')->buildSql();
        $returnWhere[] = ['id', 'exp', Db::raw("in({$subQuery2})")];
        $returnMaterialCount = db('order')->where($returnWhere)->count('id');
        //面料采购未完成单数
        $subQuery1 = db('order_material om')->join('material m', "om.material_id=m.id", 'left')->where("m.type in ('fabric', 'lining') and om.caigou_show=1 and om.is_make_tag=0 and order_id in ({$allOrders})")->distinct(true)->field('om.order_id')->buildSql();        //dump($subQuery1);
        $whereFabricNot[] = ['id', 'exp', Db::raw("in({$subQuery1})")];
        $whereFabricNot[] = ['id', 'exp', Db::raw("not in({$subQuery2})")];
        $whereFabricNot[]=["status", 'in', [0, 1]];
        $fabricCgNotCount = db('order')->where($whereFabricNot)->count('id');        //dump($fabricCgNotCount);

        //面料采购已完成单数
        $whereFabricOver[] = ['id', 'exp', Db::raw("not in({$subQuery1})")];
        $whereFabricOver[] = ['id', 'exp', Db::raw("not in({$subQuery2})")];
        $fabricCgOverCount = db('order')->where($whereFabricOver)->count('id');//dump(array_intersect($fabricCgNotCount,$returnMaterialCount));
        //未批色通过单数
        $subQuery4 = db('order_material om')->join('material m', "om.material_id=m.id", 'left')->where("m.type in ('fabric', 'lining') and om.caigou_show=1 and om.caigou_status=0")->distinct(true)->field('om.order_id')->buildSql();
        $whereApNotColor[] = ['id', 'exp', Db::raw("not in({$subQuery1})")];
        $whereApNotColor[] = ['id', 'exp', Db::raw("in({$subQuery4})")];
        $approvalColorNotCount = db('order')->where($whereApNotColor)->count('id');

        //批色不通过单数
        $subQuery5 = db('order_material om')->join('material m', "om.material_id=m.id", 'left')->where("m.type in ('fabric', 'lining') and om.caigou_show=1 and om.caigou_status=2")->distinct(true)->field('om.order_id')->buildSql();
        $whereApNoColor[] = ['id', 'exp', Db::raw("not in({$subQuery1})")];
        $whereApNoColor[] = ['id', 'exp', Db::raw("not in({$subQuery4})")];
        $whereApNoColor[] = ['id', 'exp', Db::raw("in({$subQuery5})")];
        $approvalColorNoCount = db('order')->where($whereApNoColor)->count('id');
        //已批色通过单数
        $approvalColorOverCount = $fabricCgOverCount - $approvalColorNotCount;
        //未验布缩水的单数
        $subQuery6 = db('order_material_item omi')->join('order_material om', "omi.order_material_id=om.mid", 'left')->join('material m', "om.material_id=m.id", 'left')->where("m.type in ('fabric', 'lining') and omi.is_return=0 and om.caigou_show=1 and ((om.need_checkcloth=1 and omi.is_checkcloth=0) or (om.need_shrink=1 and omi.is_shrink=0))")->distinct(true)->field('om.order_id')->buildSql();
        $whereCheckcloShrinkNot[] = ['o.id', 'exp', Db::raw("not in({$subQuery1})")];
        $whereCheckcloShrinkNot[] = ['o.id', 'exp', Db::raw("in({$subQuery6})")];
        $whereCheckcloShrinkNot[]=['o.status','in',[0,1]];
        $whereCheckcloShrinkNot[]=['o.caichuang_can_do','=',0];
        $whereCheckcloShrinkNot[]=['o.caichuang_status','=',0];
        $whereCheckcloShrinkNot[]=['om.is_make_tag','=',1];
        $whereCheckcloShrinkNot[]=[' om.tiaoshu','>',0];
        $checkcloShrinkNotCount = db('order o')
            ->join('order_material om', "o.id=om.order_id", 'left')
            ->where($whereCheckcloShrinkNot)->count();
        //已验布缩水的单数
        $checkcloShrinkOverCount = $fabricCgOverCount - $checkcloShrinkNotCount;
        //已裁床单数
        $whereCaichuangOver[] = ['id', 'exp', Db::raw("not in({$subQuery1})")];
        $whereCaichuangOver[] = ['id', 'exp', Db::raw("not in({$subQuery2})")];
        $whereCaichuangOver[] = ['id', 'exp', Db::raw("not in({$subQuery4})")];
        $whereCaichuangOver[] = ['id', 'exp', Db::raw("not in({$subQuery5})")];
        $whereCaichuangOver[] = ['caichuang_over_time', '<>', 0];
        $caichuangOverCount = db('order')->where($whereCaichuangOver)->count('id');

        //未裁床单数
//        $caichuangNotCount = $approvalColorOverCount - $caichuangOverCount - $approvalColorNoCount;
        $wherecaichuangNotCount=[
            ["o.status", 'in', [0, 1]],
            ['o.client_status', '<>', 99],
            ['o.caichuang_can_do','=',1],
            ['o.caichuang_over_time', '=', 0],
            ['om.tiaoshu','>',0],
        ];
        $caichuangNotCount = db('order o')
            ->join('order_material om', "o.id=om.order_id", 'left')
            ->where($wherecaichuangNotCount)
            ->group('o.id')
            ->count();

        //辅料发放单数
        $subQuery9 = db('distributing')->where("status in (1,2,3)")->distinct(true)->field('order_id')->buildSql();
        $whereAcOver[] = ['id', 'exp', Db::raw("not in({$subQuery1})")];
        $whereAcOver[] = ['id', 'exp', Db::raw("not in({$subQuery2})")];
        $whereAcOver[] = ['id', 'exp', Db::raw("not in({$subQuery4})")];
        $whereAcOver[] = ['id', 'exp', Db::raw("not in({$subQuery5})")];
        $whereAcOver[] = ['caichuang_over_time', '<>', 0];
        $whereAcOver[] = ['id', 'exp', Db::raw(" not in({$subQuery9})")];
        $acOverCount = db('order')->where($whereAcOver)->count('id');
        //辅料未发放单数
        $subQuery10 = db('distributing')->where("status in (1,2,3)")->distinct(true)->field('order_id')->buildSql();
        $whereAcNot[] = ['id', 'exp', Db::raw("not in({$subQuery1})")];
        $whereAcNot[] = ['id', 'exp', Db::raw("not in({$subQuery2})")];
        $whereAcNot[] = ['id', 'exp', Db::raw("not in({$subQuery4})")];
        $whereAcNot[] = ['id', 'exp', Db::raw("not in({$subQuery5})")];
        $whereAcNot[] = ['caichuang_over_time', '<>', 0];
        $whereAcNot[] = ['id', 'exp', Db::raw("in({$subQuery10})")];
        $acNotCount = db('order')->where($whereAcNot)->count('id');
        //辅料采购未完成单数
        $subQuery11 = db('order_material om')->join('material m', "om.material_id=m.id", 'left')->where("m.type = 'accessories' and om.caigou_show=1 and om.is_caigou_start!=2")->distinct(true)->field('om.order_id')->buildSql();
        $whereAcNo[] = ['id', 'exp', Db::raw("in({$subQuery11})")];
        $whereAcNo[] = ['status', 'in', [0, 1]];
        $acNoCount = db('order')->where($whereAcNo)->count('id');
        // 整理结果
        $result = array();
        $result['allCount'] = $allCount;
        $result['fabricCgOverCount'] = $fabricCgOverCount;
        $result['returnMaterialCount'] = $returnMaterialCount;
        $result['fabricCgNotCount'] = $fabricCgNotCount;
        $result['apColorOverCount'] = $approvalColorOverCount;
        $result['apColorNotCount'] = $approvalColorNotCount;
        $result['apColorNoCount'] = $approvalColorNoCount;
        $result['ckClothSkOverCount'] = $checkcloShrinkOverCount;
        $result['ckClothSkNotCount'] = $checkcloShrinkNotCount;
        $result['caichuangOverCount'] = $caichuangOverCount;
        $result['caichuangNotCount'] = $caichuangNotCount;
        $result['acOverCount'] = $acOverCount;
        $result['acNotCount'] = $acNotCount;
        $result['acNoCount'] = $acNoCount;
        if ($allCount == 0) {
            $result['fabricCgOverCountPer'] = "0%";
            $result['returnMaterialCountPer'] = "0%";
            $result['fabricCgNotCountPer'] = "0%";
            $result['apColorOverCountPer'] = "0%";
            $result['apColorNotCountPer'] = "0%";
            $result['apColorNoCountPer'] = "0%";
            $result['ckClothSkOverCountPer'] = "0%";
            $result['ckClothSkNotCountPer'] = "0%";
            $result['caichuangOverCountPer'] = "0%";
            $result['caichuangNotCountPer'] = "0%";
            $result['acOverCountPer'] = "0%";
            $result['acNotCountPer'] = "0%";
            $result['acNoCountPer'] = "0%";
        } else {
            $result['fabricCgOverCountPer'] = round($fabricCgOverCount / $allCount * 100, 2) . "%";
            $result['returnMaterialCountPer'] = round($returnMaterialCount / $allCount * 100, 2) . "%";
            $result['fabricCgNotCountPer'] = round($fabricCgNotCount / $allCount * 100, 2) . "%";
            $result['returnMaterialCountPer'] = round($returnMaterialCount / $allCount * 100, 2) . "%";
            if ($fabricCgOverCount == 0) {
                $result['apColorOverCountPer'] = "0%";
                $result['apColorNotCountPer'] = "0%";
                $result['apColorNoCountPer'] = "0%";
                $result['ckClothSkOverCountPer'] = "0%";
                $result['ckClothSkNotCountPer'] = "0%";
            } else {
                $result['apColorOverCountPer'] = round($approvalColorOverCount / $fabricCgOverCount * 100, 2) . "%";
                $result['apColorNotCountPer'] = round($approvalColorNotCount / $fabricCgOverCount * 100, 2) . "%";
                $result['apColorNoCountPer'] = round($approvalColorNoCount / $fabricCgOverCount * 100, 2) . "%";
                $result['ckClothSkOverCountPer'] = round($checkcloShrinkOverCount / $fabricCgOverCount * 100, 2) . "%";
                $result['ckClothSkNotCountPer'] = round($checkcloShrinkNotCount / $fabricCgOverCount * 100, 2) . "%";
            }
            if ($approvalColorOverCount == 0) {
                $result['caichuangOverCountPer'] = "0%";
                $result['caichuangNotCountPer'] = "0%";
            } else {
                $result['caichuangOverCountPer'] = round($caichuangOverCount / $approvalColorOverCount * 100, 2) . "%";
                $result['caichuangNotCountPer'] = round($caichuangNotCount / $approvalColorOverCount * 100, 2) . "%";
            }
            if ($caichuangOverCount == 0) {
                $result['acOverCountPer'] = "0%";
                $result['acNotCountPer'] = "0%";
            } else {
                $result['acOverCountPer'] = round($acOverCount / $caichuangOverCount * 100, 2) . "%";
                $result['acNotCountPer'] = round($acNotCount / $caichuangOverCount * 100, 2) . "%";
            }
            $result['acNoCountPer'] = round($acNoCount / $allCount * 100, 2) . "%";
        }
        $result['detailWhere'] = $detailWhere;
        return $result;
    }

    /**
     * 统计数据
     *2020/10/31
     * @return array
     */
    public function getPurchasingStatV2($factoryId = 0, $where = [], $detailWhere = [])
    {
        $cacheSuffix = ADMIN_ID;
        $allCount = $fabricCgOverCount = $fabricCgNotCount = $approvalColorOverCount = $approvalColorNotCount = $approvalColorNoCount = $checkcloShrinkOverCount = $caichuangOverCount = $caichuangNotCount = $AcOverCount = $AcNotCount = $AcNoCount = 0;
        $where[] = ['o.stock_type', 'not in', '10,11'];
        //总下单数
        $allCount = db('order o')->where($where)->count('id');
        $allOrder = db('order o')->where($where)->column('id');
        $allOrders = implode(",", $allOrder) ? implode(",", $allOrder) : 0;

        $whereAll =$wherecaichuangNotCount = $whereFabricOver = $whereFabricNot = $whereApOverColor = $whereApNotColor = $whereApNoColor = $whereCheckcloShrinkOver = $whereCheckcloShrinkNot = $whereNeedCheckcloShrink = $whereCaichuangOver = $whereCaichuangNot = $whereAcOver = $whereAcNot = $whereAcNo = $returnWhere = $where;
        //退料中单数
        $subQuery2 = db('material_return')->where("status=1 and order_id in ({$allOrders})")->distinct(true)->field('order_id')->buildSql();
        $returnWhere[] = ['o.id', 'exp', Db::raw("in({$subQuery2})")];
        $returnMaterialCount = db('order o')->where($returnWhere)->count('id');
        //面料采购未完成单数
        $subQuery1 = db('order_material om')->join('material m', "om.material_id=m.id", 'left')->where("m.type in ('fabric', 'lining') and om.caigou_show=1 and om.is_make_tag=0 and order_id in ({$allOrders})")->distinct(true)->field('om.order_id')->buildSql();        //dump($subQuery1);
        $whereFabricNot[] = ['o.id', 'exp', Db::raw("in({$subQuery1})")];
        $whereFabricNot[] = ['o.id', 'exp', Db::raw("not in({$subQuery2})")];
        $whereFabricNot[]=["o.status", 'in', [0, 1]];
        $fabricCgNotCount = db('order o')->where($whereFabricNot)->count('id');        //dump($fabricCgNotCount);

        //面料采购已完成单数
        $whereFabricOver[] = ['o.id', 'exp', Db::raw("not in({$subQuery1})")];
        $whereFabricOver[] = ['o.id', 'exp', Db::raw("not in({$subQuery2})")];
        $fabricCgOverCount = db('order o')->where($whereFabricOver)->count('id');//dump(array_intersect($fabricCgNotCount,$returnMaterialCount));
        //未批色通过单数
        $subQuery4 = db('order_material om')->join('material m', "om.material_id=m.id", 'left')->where("m.type in ('fabric', 'lining') and om.caigou_show=1 and om.caigou_status=0")->distinct(true)->field('om.order_id')->buildSql();
        $whereApNotColor[] = ['o.id', 'exp', Db::raw("not in({$subQuery1})")];
        $whereApNotColor[] = ['o.id', 'exp', Db::raw("in({$subQuery4})")];
        $approvalColorNotCount = db('order o')->where($whereApNotColor)->count('id');

        //批色不通过单数
        $subQuery5 = db('order_material om')->join('material m', "om.material_id=m.id", 'left')->where("m.type in ('fabric', 'lining') and om.caigou_show=1 and om.caigou_status=2")->distinct(true)->field('om.order_id')->buildSql();
        $whereApNoColor[] = ['o.id', 'exp', Db::raw("not in({$subQuery1})")];
        $whereApNoColor[] = ['o.id', 'exp', Db::raw("not in({$subQuery4})")];
        $whereApNoColor[] = ['o.id', 'exp', Db::raw("in({$subQuery5})")];
        $approvalColorNoCount = db('order o')->where($whereApNoColor)->count('id');
        //已批色通过单数
        $approvalColorOverCount = $fabricCgOverCount - $approvalColorNotCount;
        //未验布缩水的单数
        $subQuery6 = db('order_material_item omi')->join('order_material om', "omi.order_material_id=om.mid", 'left')->join('material m', "om.material_id=m.id", 'left')->where("m.type in ('fabric', 'lining') and omi.is_return=0 and om.caigou_show=1 and ((om.need_checkcloth=1 and omi.is_checkcloth=0) or (om.need_shrink=1 and omi.is_shrink=0))")->distinct(true)->field('om.order_id')->buildSql();
        $whereCheckcloShrinkNot[] = ['o.id', 'exp', Db::raw("not in({$subQuery1})")];
        $whereCheckcloShrinkNot[] = ['o.id', 'exp', Db::raw("in({$subQuery6})")];
        $whereCheckcloShrinkNot[]=['o.status','in',[0,1]];
        $whereCheckcloShrinkNot[]=['o.caichuang_can_do','=',0];
        $whereCheckcloShrinkNot[]=['o.caichuang_status','=',0];
        $whereCheckcloShrinkNot[]=['om.is_make_tag','=',1];
        $whereCheckcloShrinkNot[]=[' om.tiaoshu','>',0];
        $checkcloShrinkNotCount = db('order o')
            ->join('order_material om', "o.id=om.order_id", 'left')
            ->where($whereCheckcloShrinkNot)->count();
        //已验布缩水的单数
        $checkcloShrinkOverCount = $fabricCgOverCount - $checkcloShrinkNotCount;
        //已裁床单数
        $whereCaichuangOver[] = ['o.id', 'exp', Db::raw("not in({$subQuery1})")];
        $whereCaichuangOver[] = ['o.id', 'exp', Db::raw("not in({$subQuery2})")];
        $whereCaichuangOver[] = ['o.id', 'exp', Db::raw("not in({$subQuery4})")];
        $whereCaichuangOver[] = ['o.id', 'exp', Db::raw("not in({$subQuery5})")];
        $whereCaichuangOver[] = ['o.caichuang_over_time', '<>', 0];
        $caichuangOverCount = db('order o')->where($whereCaichuangOver)->count('id');

        //未裁床单数
//        $caichuangNotCount = $approvalColorOverCount - $caichuangOverCount - $approvalColorNoCount;
        $wherecaichuangNotCount[]=["o.status", 'in', [0, 1]];
        $wherecaichuangNotCount[]=['o.client_status', '<>', 99];
        $wherecaichuangNotCount[]=['o.caichuang_can_do','=',1];
        $wherecaichuangNotCount[]=['o.caichuang_over_time', '=', 0];
        $wherecaichuangNotCount[]= ['om.tiaoshu','>',0];
        $caichuangNotCount = db('order o')
            ->join('order_material om', "o.id=om.order_id", 'left')
            ->where($wherecaichuangNotCount)
            ->group('o.id')
            ->count();

        //辅料发放单数
        $subQuery9 = db('distributing')->where("status in (1,2,3)")->distinct(true)->field('order_id')->buildSql();
        $whereAcOver[] = ['o.id', 'exp', Db::raw("not in({$subQuery1})")];
        $whereAcOver[] = ['o.id', 'exp', Db::raw("not in({$subQuery2})")];
        $whereAcOver[] = ['o.id', 'exp', Db::raw("not in({$subQuery4})")];
        $whereAcOver[] = ['o.id', 'exp', Db::raw("not in({$subQuery5})")];
        $whereAcOver[] = ['o.caichuang_over_time', '<>', 0];
        $whereAcOver[] = ['o.id', 'exp', Db::raw(" not in({$subQuery9})")];
        $acOverCount = db('order o')->where($whereAcOver)->count('id');
        //辅料未发放单数
        $subQuery10 = db('distributing')->where("status in (1,2,3)")->distinct(true)->field('order_id')->buildSql();
        $whereAcNot[] = ['o.id', 'exp', Db::raw("not in({$subQuery1})")];
        $whereAcNot[] = ['o.id', 'exp', Db::raw("not in({$subQuery2})")];
        $whereAcNot[] = ['o.id', 'exp', Db::raw("not in({$subQuery4})")];
        $whereAcNot[] = ['o.id', 'exp', Db::raw("not in({$subQuery5})")];
        $whereAcNot[] = ['o.caichuang_over_time', '<>', 0];
        $whereAcNot[] = ['o.id', 'exp', Db::raw("in({$subQuery10})")];
        $acNotCount = db('order o')->where($whereAcNot)->count('id');
        //辅料采购未完成单数
        $subQuery11 = db('order_material om')->join('material m', "om.material_id=m.id", 'left')->where("m.type = 'accessories' and om.caigou_show=1 and om.is_caigou_start!=2")->distinct(true)->field('om.order_id')->buildSql();
        $whereAcNo[] = ['o.id', 'exp', Db::raw("in({$subQuery11})")];
        $whereAcNo[] = ['o.status', 'in', [0, 1]];
        $acNoCount = db('order o')->where($whereAcNo)->count('id');
        // 整理结果
        $result = array();
        $result['allCount'] = $allCount;
        $result['fabricCgOverCount'] = $fabricCgOverCount;
        $result['returnMaterialCount'] = $returnMaterialCount;
        $result['fabricCgNotCount'] = $fabricCgNotCount;
        $result['apColorOverCount'] = $approvalColorOverCount;
        $result['apColorNotCount'] = $approvalColorNotCount;
        $result['apColorNoCount'] = $approvalColorNoCount;
        $result['ckClothSkOverCount'] = $checkcloShrinkOverCount;
        $result['ckClothSkNotCount'] = $checkcloShrinkNotCount;
        $result['caichuangOverCount'] = $caichuangOverCount;
        $result['caichuangNotCount'] = $caichuangNotCount;
        $result['acOverCount'] = $acOverCount;
        $result['acNotCount'] = $acNotCount;
        $result['acNoCount'] = $acNoCount;
        if ($allCount == 0) {
            $result['fabricCgOverCountPer'] = "0%";
            $result['returnMaterialCountPer'] = "0%";
            $result['fabricCgNotCountPer'] = "0%";
            $result['apColorOverCountPer'] = "0%";
            $result['apColorNotCountPer'] = "0%";
            $result['apColorNoCountPer'] = "0%";
            $result['ckClothSkOverCountPer'] = "0%";
            $result['ckClothSkNotCountPer'] = "0%";
            $result['caichuangOverCountPer'] = "0%";
            $result['caichuangNotCountPer'] = "0%";
            $result['acOverCountPer'] = "0%";
            $result['acNotCountPer'] = "0%";
            $result['acNoCountPer'] = "0%";
        } else {
            $result['fabricCgOverCountPer'] = round($fabricCgOverCount / $allCount * 100, 2) . "%";
            $result['returnMaterialCountPer'] = round($returnMaterialCount / $allCount * 100, 2) . "%";
            $result['fabricCgNotCountPer'] = round($fabricCgNotCount / $allCount * 100, 2) . "%";
            $result['returnMaterialCountPer'] = round($returnMaterialCount / $allCount * 100, 2) . "%";
            if ($fabricCgOverCount == 0) {
                $result['apColorOverCountPer'] = "0%";
                $result['apColorNotCountPer'] = "0%";
                $result['apColorNoCountPer'] = "0%";
                $result['ckClothSkOverCountPer'] = "0%";
                $result['ckClothSkNotCountPer'] = "0%";
            } else {
                $result['apColorOverCountPer'] = round($approvalColorOverCount / $fabricCgOverCount * 100, 2) . "%";
                $result['apColorNotCountPer'] = round($approvalColorNotCount / $fabricCgOverCount * 100, 2) . "%";
                $result['apColorNoCountPer'] = round($approvalColorNoCount / $fabricCgOverCount * 100, 2) . "%";
                $result['ckClothSkOverCountPer'] = round($checkcloShrinkOverCount / $fabricCgOverCount * 100, 2) . "%";
                $result['ckClothSkNotCountPer'] = round($checkcloShrinkNotCount / $fabricCgOverCount * 100, 2) . "%";
            }
            if ($approvalColorOverCount == 0) {
                $result['caichuangOverCountPer'] = "0%";
                $result['caichuangNotCountPer'] = "0%";
            } else {
                $result['caichuangOverCountPer'] = round($caichuangOverCount / $approvalColorOverCount * 100, 2) . "%";
                $result['caichuangNotCountPer'] = round($caichuangNotCount / $approvalColorOverCount * 100, 2) . "%";
            }
            if ($caichuangOverCount == 0) {
                $result['acOverCountPer'] = "0%";
                $result['acNotCountPer'] = "0%";
            } else {
                $result['acOverCountPer'] = round($acOverCount / $caichuangOverCount * 100, 2) . "%";
                $result['acNotCountPer'] = round($acNotCount / $caichuangOverCount * 100, 2) . "%";
            }
            $result['acNoCountPer'] = round($acNoCount / $allCount * 100, 2) . "%";
        }
        $result['detailWhere'] = $detailWhere;
        return $result;
    }

    /**
     * 统计信息>>订单时效
     * @author zhuguosen 905502314@qq.com
     * @date 2022/4/14
     */
    public function statistics_order_shixiao($param)
    {
        switch ($param['type']) {
            case 'order_shixiao'://订单时效
                $res = [
                    0 => [
                        'sql' => " and sp.order_create_time_0>= 1649865600",//采购时效 2022-04-14 1649865600 下的单 才统计
                    ],
                    1 => [
                        'sql' => " and sp.caigou_over_time>= 1648742400",//面料处理时效 2022-04-01 采购完成才统计
                    ],
                    2 => [
                        'sql' => " and sp.caichuang_can_do_time_1>= 1648742400",//裁床时效 2022-04-01 可裁剪 才统计
                    ],
                    3 => [
                        'sql' => " and sp.order_create_time_0>= 1648742400",//缝前时效 2022-04-01 下的单 才统计
                    ],
                ];
                foreach ($res as $k => $v) {
                    $sql = "SELECT 
sum(IF(sp.flag_{$k}=0 and sp.finish_day_{$k}<1 and sp.finish_day_{$k}>0,1,0)) as day_1
,sum(IF(sp.flag_{$k}=0 and sp.finish_day_{$k}<2 and sp.finish_day_{$k}>=1,1,0)) as day_2
,sum(IF(sp.flag_{$k}=0 and sp.finish_day_{$k}<3 and sp.finish_day_{$k}>=2,1,0)) as day_3
,sum(IF(sp.flag_{$k}=0 and sp.finish_day_{$k}<4 and sp.finish_day_{$k}>=3,1,0)) as day_4
,sum(IF(sp.flag_{$k}=0 and sp.finish_day_{$k}>=4,1,0)) as day_5
,sum(IF(sp.flag_{$k}=0 and sp.finish_day_{$k}=-1 ,1,0)) as day_not
FROM wx_statistics_purchasing sp
WHERE  sp.`status` in (0,1,2) and sp.client_status <>99 and sp.stock_type <>10
{$v['sql']}
";
                    $temp = db()->query($sql)[0];
                    $temp['day_1_baifeng'] = ($temp['day_1'] + $temp['day_not']) > 0 ? sprintf("%.2f", ($temp['day_1'] / ($temp['day_1'] + $temp['day_not'])) * 100) : 0.00;
                    $temp['day_2_baifeng'] = ($temp['day_2'] + $temp['day_not']) > 0 ? sprintf("%.2f", ($temp['day_2'] / ($temp['day_2'] + $temp['day_not'])) * 100) : 0.00;
                    $temp['day_3_baifeng'] = ($temp['day_3'] + $temp['day_not']) > 0 ? sprintf("%.2f", ($temp['day_3'] / ($temp['day_3'] + $temp['day_not'])) * 100) : 0.00;
                    $temp['day_4_baifeng'] = ($temp['day_4'] + $temp['day_not']) > 0 ? sprintf("%.2f", ($temp['day_4'] / ($temp['day_4'] + $temp['day_not'])) * 100) : 0.00;
                    $temp['day_5_baifeng'] = ($temp['day_5'] + $temp['day_not']) > 0 ? sprintf("%.2f", ($temp['day_5'] / ($temp['day_5'] + $temp['day_not'])) * 100) : 0.00;
                    $res[$k] = $temp;
                }
                return $res;
                break;
            case 'material_shixiao'://时效统计
                $res = [
                    0 => [
                        'sql' => " and sp.arrive_time>= 1648742400",//面料打标时效 2022-04-01 到料--采购完成的才统计
                    ],
                    1 => [
                        'sql' => " and sp.caigou_start_time>= 1649865600",//辅料打标时效  2022-04-14 点击“开始采购”--采购完成 的才统计
                    ],
                    2 => [
                        'sql' => " and sp.arrive_time>= 1649865600",//批色时效 2022-04-14  到料--已批色，未批过也计算批色--第一次按的时间
                    ],
                    3 => [
                        'sql' => " and sp.arrive_time>= 1648742400",//验布时效 2022-04-01 点击“打标”--面料工序“验布为Y”
                    ],
                    4 => [
                        'sql' => " and ( 
                        (sp.need_checkcloth=0  and sp.make_tag_time_first>1648742400 ) 
                        OR (sp.need_checkcloth=1  and sp.checkcloth_over_time >1648742400 )
                         )
                         ",//缩水时效 2022-04-01 缩水条件：验布完成，点击“打标--面料工序“缩水为Y”
                    ],
                ];
                foreach ($res as $k => $v) {
                    $sql = "SELECT 
sum(IF(sp.flag_{$k}=0 and sp.finish_day_{$k}<12 and sp.finish_day_{$k}>0,1,0)) as day_1
,sum(IF(sp.flag_{$k}=0 and sp.finish_day_{$k}<24 and sp.finish_day_{$k}>=12,1,0)) as day_2
,sum(IF(sp.flag_{$k}=0 and sp.finish_day_{$k}<36 and sp.finish_day_{$k}>=24,1,0)) as day_3
,sum(IF(sp.flag_{$k}=0 and sp.finish_day_{$k}<48 and sp.finish_day_{$k}>=36,1,0)) as day_4
,sum(IF(sp.flag_{$k}=0 and sp.finish_day_{$k}>=48,1,0)) as day_5
,sum(IF(sp.flag_{$k}=0 and sp.finish_day_{$k}=-1 ,1,0)) as day_not
FROM wx_statistics_purchasing_material sp
WHERE  sp.`status` in (0,1,2) and sp.client_status <>99 and sp.stock_type <>10
{$v['sql']}
";
                    $temp = db()->query($sql)[0];
                    $temp['day_1_baifeng'] = ($temp['day_1'] + $temp['day_not']) > 0 ? sprintf("%.2f", ($temp['day_1'] / ($temp['day_1'] + $temp['day_not'])) * 100) : 0.00;
                    $temp['day_2_baifeng'] = ($temp['day_2'] + $temp['day_not']) > 0 ? sprintf("%.2f", ($temp['day_2'] / ($temp['day_2'] + $temp['day_not'])) * 100) : 0.00;
                    $temp['day_3_baifeng'] = ($temp['day_3'] + $temp['day_not']) > 0 ? sprintf("%.2f", ($temp['day_3'] / ($temp['day_3'] + $temp['day_not'])) * 100) : 0.00;
                    $temp['day_4_baifeng'] = ($temp['day_4'] + $temp['day_not']) > 0 ? sprintf("%.2f", ($temp['day_4'] / ($temp['day_4'] + $temp['day_not'])) * 100) : 0.00;
                    $temp['day_5_baifeng'] = ($temp['day_5'] + $temp['day_not']) > 0 ? sprintf("%.2f", ($temp['day_5'] / ($temp['day_5'] + $temp['day_not'])) * 100) : 0.00;
                    $res[$k] = $temp;
                }
                return $res;
                break;
        }
    }

    public function statistics_deatail($param)
    {
        switch ($param['type']) {
            case 'order_shixiao'://订单时效
                list($where, $whereRaw) = $this->_statistics_deatail_where($param);
                $list = db('statistics_purchasing sp')
                    ->field('sp.*')
                    ->whereRaw($whereRaw)
                    ->page($param['page'])
                    ->limit($param['limit'])
                    ->select();
                $count = db('statistics_purchasing sp')
                    ->field('sp.*')
                    ->whereRaw($whereRaw)
                    ->count();
                $list = $this->_statistics_deatail_list($param, $list);
                return ['code' => 0, 'msg' => '', 'data' => $list, 'count' => $count];
                break;
            case 'material_shixiao'://面料时效统计
                list($where, $whereRaw) = $this->_statistics_deatail_where($param);
                $list = db('statistics_purchasing_material sp')
                    ->field('sp.*')
                    ->whereRaw($whereRaw)
                    ->page($param['page'])
                    ->limit($param['limit'])
                    ->select();
                $count = db('statistics_purchasing_material sp')
                    ->field('sp.*')
                    ->whereRaw($whereRaw)
                    ->count();
                $list = $this->_statistics_deatail_list($param, $list);
                return ['code' => 0, 'msg' => '', 'data' => $list, 'count' => $count];
                break;
        }
    }

    private function _statistics_deatail_where($param)
    {
        switch ($param['type']) {
            case 'order_shixiao'://订单时效
                $res = [
                    0 => [
                        'sql' => " and sp.order_create_time_0>= 1649865600",//采购时效 2022-04-14 1649865600 下的单 才统计
//                'sql_array' => [
//                    1 => " and sp.flag_0=0 and sp.finish_day_0<1 and sp.finish_day_0>0",
//                    2 => " and sp.flag_0=0 and sp.finish_day_0<2 and sp.finish_day_0>=1",
//                    3 => " and sp.flag_0=0 and sp.finish_day_0<3 and sp.finish_day_0>=2",
//                    4 => " and sp.flag_0=0 and sp.finish_day_0<4 and sp.finish_day_0>=3",
//                    5 => " and sp.flag_0=0 and sp.finish_day_0>=4",
//                    -1 => " and sp.flag_0=0 and sp.finish_day_0=-1 ",
//                ],
                    ],
                    1 => [
                        'sql' => " and sp.caigou_over_time>= 1648742400",//面料处理时效 2022-04-01 采购完成才统计

                    ],
                    2 => [
                        'sql' => " and sp.caichuang_can_do_time_1>= 1648742400",//裁床时效 2022-04-01 可裁剪 才统计

                    ],
                    3 => [
                        'sql' => " and sp.order_create_time_0>= 1648742400",//缝前时效 2022-04-01 下的单 才统计

                    ],
                ];
                $k = $param['num'];
                $day_sql = [
                    1 => " and sp.flag_{$k}=0 and sp.finish_day_{$k}<1 and sp.finish_day_{$k}>0",
                    2 => " and sp.flag_{$k}=0 and sp.finish_day_{$k}<2 and sp.finish_day_{$k}>=1",
                    3 => " and sp.flag_{$k}=0 and sp.finish_day_{$k}<3 and sp.finish_day_{$k}>=2",
                    4 => " and sp.flag_{$k}=0 and sp.finish_day_{$k}<4 and sp.finish_day_{$k}>=3",
                    5 => " and sp.flag_{$k}=0 and sp.finish_day_{$k}>=4",
                    -1 => " and sp.flag_{$k}=0 and sp.finish_day_{$k}=-1 ",
                ];
                $where = [];
                $whereRaw = " sp.`status` in (0,1,2) and sp.client_status <>99 and sp.stock_type <>10 
        {$res[$param['num']]['sql']} {$day_sql[$param['day']]} ";
                return [
                    $where,
                    $whereRaw
                ];
                break;
            case 'material_shixiao'://订单时效
                $res = [
                    0 => [
                        'sql' => " and sp.arrive_time>= 1648742400",//面料打标时效 2022-04-01 到料--采购完成的才统计
                    ],
                    1 => [
                        'sql' => " and sp.caigou_start_time>= 1649865600",//辅料打标时效  2022-04-14 点击“开始采购”--采购完成 的才统计
                    ],
                    2 => [
                        'sql' => " and sp.arrive_time>= 1649865600",//批色时效 2022-04-14  到料--已批色，未批过也计算批色--第一次按的时间
                    ],
                    3 => [
                        'sql' => " and sp.arrive_time>= 1648742400",//验布时效 2022-04-01 点击“打标”--面料工序“验布为Y”
                    ],
                    4 => [
                        'sql' => " and ( 
                        (sp.need_checkcloth=0  and sp.make_tag_time_first>1648742400 ) 
                        OR (sp.need_checkcloth=1  and sp.checkcloth_over_time >1648742400 )
                         )
                         ",//缩水时效 2022-04-01 缩水条件：验布完成，点击“打标--面料工序“缩水为Y”
                    ],
                ];
                $k = $param['num'];
                $day_sql = [
                    1 => " and sp.flag_{$k}=0 and sp.finish_day_{$k}<12 and sp.finish_day_{$k}>0",
                    2 => " and sp.flag_{$k}=0 and sp.finish_day_{$k}<24 and sp.finish_day_{$k}>=12",
                    3 => " and sp.flag_{$k}=0 and sp.finish_day_{$k}<36 and sp.finish_day_{$k}>=24",
                    4 => " and sp.flag_{$k}=0 and sp.finish_day_{$k}<48 and sp.finish_day_{$k}>=36",
                    5 => " and sp.flag_{$k}=0 and sp.finish_day_{$k}>=48",
                    -1 => " and sp.flag_{$k}=0 and sp.finish_day_{$k}=-1 ",
                ];
                $where = [];
                $whereRaw = " sp.`status` in (0,1,2) and sp.client_status <>99 and sp.stock_type <>10 
        {$res[$param['num']]['sql']} {$day_sql[$param['day']]} ";
                return [
                    $where,
                    $whereRaw
                ];
                break;
        }
    }

    private function _statistics_deatail_list($param, $list)
    {
        switch ($param['type']) {
            case 'order_shixiao':
                $res = [
                    0 => [//采购时效 2022-04-14 1649865600 下的单 才统计
                        'start_date' => 'order_create_time',
                        'end_date' => 'caiguo_time_0',
                    ],
                    1 => [//面料处理时效 2022-04-01 采购完成才统计
                        'start_date' => 'caigou_over_time',
                        'end_date' => 'caichuang_can_do_time_0',
                    ],
                    2 => [//裁床时效 2022-04-01 可裁剪 才统计
                        'start_date' => 'caichuang_can_do_time_1',
                        'end_date' => 'caichuang_over_time',
                    ],
                    3 => [//缝前时效 2022-04-01 下的单 才统计
                        'start_date' => 'order_create_time',
                        'end_date' => 'caichuang_over_time',
                    ],
                ];
                foreach ($list as $k => $v) {
                    $list[$k]['start_date'] = $v[$res[$param['num']]['start_date']] > 0 ? date('Y-m-d H:i:s', $v[$res[$param['num']]['start_date']]) : '';
                    $list[$k]['end_date'] = $v[$res[$param['num']]['end_date']] > 0 ? date('Y-m-d H:i:s', $v[$res[$param['num']]['end_date']]) : '';
                }
                return $list;
                break;
            case 'material_shixiao':
                $res = [
                    0 => [//面料打标时效 2022-04-01 到料--采购完成的才统计
                        'start_date' => 'arrive_time',
                        'end_date' => 'caigou_finish_time',
                    ],
                    1 => [//辅料打标时效  2022-04-14 点击“开始采购”--采购完成 的才统计
                        'start_date' => 'caigou_start_time',
                        'end_date' => 'caigou_finish_time',
                    ],
                    2 => [//批色时效 2022-04-14  到料--已批色，未批过也计算批色--第一次按的时间
                        'start_date' => 'arrive_time',
                        'end_date' => 'caigou_first_time',
                    ],
                    3 => [//验布时效 2022-04-01 点击“打标”--面料工序“验布为Y”
                        'start_date' => 'make_tag_time_first',
                        'end_date' => 'checkcloth_over_time',
                    ],
                    4 => [//缩水时效 2022-04-01 缩水条件：验布完成，点击“打标--面料工序“缩水为Y”
                        'start_date' => '',//need_checkcloth make_tag_time_first checkcloth_over_time
                        'end_date' => 'shrink_over_time',
                    ],
                ];
                foreach ($list as $k => $v) {
                    if ($param['num'] < 4) {
                        $list[$k]['start_date'] = $v[$res[$param['num']]['start_date']] > 0 ? date('Y-m-d H:i:s', $v[$res[$param['num']]['start_date']]) : '';
                        $list[$k]['end_date'] = $v[$res[$param['num']]['end_date']] > 0 ? date('Y-m-d H:i:s', $v[$res[$param['num']]['end_date']]) : '';
                    }
                    if ($param['num'] == 4) {
                        if ($v['need_checkcloth'] == 0) {
                            $list[$k]['start_date'] = $v['make_tag_time_first'] > 0 ? date('Y-m-d H:i:s', $v['make_tag_time_first']) : '';
                        }
                        if ($v['need_checkcloth'] == 1) {
                            $list[$k]['start_date'] = $v['checkcloth_over_time'] > 0 ? date('Y-m-d H:i:s', $v['checkcloth_over_time']) : '';
                        }
                        $list[$k]['end_date'] = $v[$res[$param['num']]['end_date']] > 0 ? date('Y-m-d H:i:s', $v[$res[$param['num']]['end_date']]) : '';
                    }
                }
                return $list;
                break;
        }
    }
}

